import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@abp/components';
import * as moment from 'moment';
import { AppConsts } from '@abp/AppConsts';
import { FileDownloadService } from '@abp/utils/file-download.service';
import { RoleServiceProxy } from '@abp/service-proxies/service-proxies';
import { RoleCreateOrEditComponent } from './role-create-or-edit.component';

@Component({
    templateUrl: './role.component.html',
    styleUrls: ['./role.component.less'],
})
export class RoleComponent extends AppComponentBase {

    selectedPermission = '';

    /** 排序 */
    sortValue = null;
    sortKey = null;

    /** 加载 */
    skeletonLoading = true;
    switchloading = false;

    constructor(
        injector: Injector,
        private roleService: RoleServiceProxy,
        private fileDownloadService: FileDownloadService,
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.searchData();
    }

    sort(sort: { key: string, value: string }): void {
        this.tableHelper.sort = sort;
        this.searchData();
    }

    searchData(reset: boolean = false): void {
        if (reset) {
            this.tableHelper.pageIndex = 1;
        }
        this.tableHelper.isLoading = true;
        let permission = this.permission ? this.selectedPermission : undefined;
        this.roleService.getRoles(permission).subscribe((data) => {
            this.tableHelper.isLoading = false;
            this.tableHelper.totalRecordsCount = data.items.length;
            this.tableHelper.records = data.items;
            console.log(this.tableHelper);
        });
    }

    createBuildingPlan(): void {
        let config = this.drawerConfig(
            '创建',
            RoleCreateOrEditComponent,
            {
                id: undefined
            },500)
        const drawerRef = this.drawerService.create(config);

        drawerRef.afterOpen.subscribe(() => {

        });

        drawerRef.afterClose.subscribe(data => {
            if (data) {
                this.searchData();
            }
        });
    }

    editBuildingPlan(id: number): void {
        let config = this.drawerConfig(
            '编辑',
            RoleCreateOrEditComponent,
            {
                id: id
            },500)
        const drawerRef = this.drawerService.create(config);

        drawerRef.afterOpen.subscribe(() => {

        });

        drawerRef.afterClose.subscribe(data => {
            if (data) {
                this.searchData();
            }
        });
    }

    deleteConfirm(id: number): void {
        this.roleService.deleteRole(id).subscribe(() => {
            this.searchData();
            this.notify.success('删除成功！');
        });
    }
}

